package com.cskaoyan.day20;

import java.io.*;

public class type2 {
    public static void main(String[] args) {
        Thread t1 =new Thread(new task1(),"线程1");
        Thread t2 =new Thread(new task2(),"线程2");
        Thread t3 =new Thread(new task3(),"线程3");
        t1.start();
        t2.start();
        t3.start();
    }
}

class task1 implements Runnable {
    static public long time1 = System.currentTimeMillis();

    @Override
    public void run() {
        try (BufferedInputStream in = new BufferedInputStream(new FileInputStream("a.txt"));
             BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("d:/a.txt"))) {
            int d;
            double count = 0;
            while ((d = in.read()) != -1) {
                out.write(d);
                count++;
                if (count % 200 == 0) {
                    double res = ((count * 100) / (new File("a.txt").length()));
                    System.out.println(Thread.currentThread().getName() + ":“文件已复制" + res + "%");
                }
            }


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+ ":“文件已复制完成！");
    }
}

class task2 implements Runnable {
    static public long time1 = System.currentTimeMillis();

    @Override
    public void run() {
        try (BufferedInputStream in = new BufferedInputStream(new FileInputStream("a.txt"));
             BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("d:/a1.txt"))) {
            int d;
            double count = 0;
            while ((d = in.read()) != -1) {
                out.write(d);
                count++;
                if (count % 200 == 0) {

                    double res = ((count * 100) / (new File("a.txt").length()));
                    System.out.println(Thread.currentThread().getName() + ":“文件已复制" + res + "%");
                }
            }


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+ ":“文件已复制完成！");
    }
}

class task3 implements Runnable {
    static public long time1 = System.currentTimeMillis();

    @Override
    public void run() {
        try (BufferedInputStream in = new BufferedInputStream(new FileInputStream("a.txt"));
             BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("d:/a2.txt"))) {
            int d;
            double count = 0;
            while ((d = in.read()) != -1) {
                out.write(d);
                count++;
                if (count % 200 == 0) {

                    double res = ((count * 100) / (new File("a.txt").length()));
                    System.out.println(Thread.currentThread().getName() + ":“文件已复制" + res + "%");
                }
            }


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+ ":“文件已复制完成！");
    }
}